import router from './router'
import store from './store'
import {Message} from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from '@/utils/auth'
import Cookies from 'js-cookie'

NProgress.configure({showSpinner: false});

const whiteList = ['/login', '/auth-redirect', '/bind', '/register'];


router.beforeEach((to, from, next) => {
  NProgress.start();
  if (getToken()) {
    /* has token*/
    if (to.path === '/login') {
      next({path: '/'});
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => {
          // 拉取user_info
          const roles = res.roles;
          store.dispatch('GenerateRoutes', {roles}).then(accessRoutes => {
            let activeMenuId = Cookies.get('activeMenuId');
            let bigScreen = [];
            if(activeMenuId == 0 || activeMenuId == undefined || activeMenuId == null) {
              // 产品碳足迹
              bigScreen = [
                {
                  path: '/bigScreen',//首页
                  component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                  name: 'bigScreen/index',
                  hidden: true,
                  children: [
                    {
                      path: 'productCarbon',
                      hidden: true,
                      name: 'b_productCarbon',
                      component: (resolve) => require(['@/views/bigScreen/carbon/productCarbon/index'], resolve),
                      meta: { title: '碳足迹计算', icon: 'dashboard', noCache: true, affix: true, bigScreen: true }
                    },
                    {
                      path: 'product',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                      name: 'b_product',
                      meta: { title: '产品配置', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                      children: [{
                        path: 'productCategorize',
                        hidden: true,
                        name: 'b_productCategorize',
                        meta: { title: '产品分类', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/productCategorize/index'], resolve),
                      },{
                        path: 'standardStage',
                        hidden: true,
                        name: 'b_standardStage',
                        meta: { title: '标准阶段', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/standardStage/index'], resolve),
                      }]
                    },
                    {
                      path: 'categorize',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                      name: 'b_categorize',
                      meta: { title: '因子库配置', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                      children: [{
                        path: 'categorizeInfo',
                        hidden: true,
                        name: 'b_categorizeInfo',
                        meta: { title: '因子特征要素', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/categorizeInfo/index'], resolve),
                      },{
                        path: 'carbonFactorInfo',
                        hidden: true,
                        name: 'b_carbonFactorInfo',
                        meta: { title: '碳排放因子库', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/carbonFactorInfo/index'], resolve),
                      }]
                    },
                    {
                      path: 'productInfoAll',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                      name: 'b_productInfoAll',
                      meta: { title: '碳排放核算', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                      children: [{
                        path: 'productInfo',
                        hidden: true,
                        name: 'b_productInfo',
                        meta: { title: '产品周期要素', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/productInfo/index'], resolve),
                      },{
                        path: 'emissionPathLog',
                        hidden: true,
                        name: 'b_emissionPathLog',
                        meta: { title: '减排路径记录', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/carbon/emissionPathLog/index'], resolve),
                      },{
                        path: 'productCarbonConChart',
                        hidden: true,
                        name: 'b_productCarbonConChart',
                        meta: { title: '历史查询', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/carbon/productCarbon/ProductCarbonConChart'], resolve),
                      }]
                    },
                  ]
                }
              ]
              store.commit('SET_BIG_SCREEN', bigScreen);
            } else if (activeMenuId == 1) {
              // 组织碳管理
              bigScreen = [
              {
                  path: '/bigScreen',//首页
                  component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                  name: 'bigScreen/index',
                  hidden: true,
                  children: [
                    {
                      path: 'project1',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/project/index'], resolve),
                      name: 'b_project1',
                      meta: { title: '建设过程碳排放', icon: 'dashboard', noCache: true, affix: true, bigScreen: true }
                    },
                    {
                      path: 'carbonAnalysis',
                      hidden: true,
                      name: 'b_carbonAnalysis',
                      meta: { title: '运行过程碳排放', icon: 'monitor', noCache: true, affix: true, bigScreen: true },
                      component: (resolve) => require(['@/views/bigScreen/runCarbonEmission/index'], resolve),
                    }
                  ]
                }
              ];
              store.commit('SET_BIG_SCREEN', bigScreen);
            } else if (activeMenuId == 2) {
            // CCER项目
            bigScreen = [
                {
                  path: '/bigScreen',//首页
                  component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                  name: 'bigScreen/index',
                  hidden: true,
                  children: [
                    {
                      path: 'product2',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/ccerProject/index'], resolve),
                      name: 'b_product2',
                      meta: { title: '项目管理', icon: 'dashboard', noCache: true, affix: true, bigScreen: true }
                    },
                    {
                      path: 'categorize',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                      name: 'b_categorize',
                      meta: { title: '减排交易市场', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                      children: [{
                        path: 'announceIng',
                        hidden: true,
                        name: 'b_announceIng',
                        meta: { title: '公示中项目', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/announceIng'], resolve),
                      },{
                        path: 'announceEng',
                        hidden: true,
                        name: 'b_announceEng',
                        meta: { title: '公示结束项目', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/announceEng'], resolve),
                      },{
                        path: 'registered',
                        hidden: true,
                        name: 'b_registered',
                        meta: { title: '已登记项目', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/registered'], resolve),
                      },{
                        path: 'announceIngReduce',
                        hidden: true,
                        name: 'b_announceIngReduce',
                        meta: { title: '公示中减排量', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/announceIngReduce'], resolve),
                      },{
                        path: 'announceEndReduce',
                        hidden: true,
                        name: 'b_announceEndReduce',
                        meta: { title: '公示结束减排量', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/announceEndReduce'], resolve),
                      },{
                        path: 'registeredReduce',
                        hidden: true,
                        name: 'b_registeredReduce',
                        meta: { title: '已登记减排量', icon: 'report', noCache: true, affix: true, bigScreen: true },
                        component: (resolve) => require(['@/views/bigScreen/ccerProject/registeredReduce'], resolve),
                      }]
                    },
                  ]
                }
              ]
              store.commit('SET_BIG_SCREEN', bigScreen);
            } else if (activeMenuId == 3) {
            // 绿证管理
            bigScreen = [
                {
                  path: '/bigScreen',//首页
                  component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
                  name: 'bigScreen/index',
                  hidden: true,
                  children: [
                    {
                      path: 'product3',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/greenCertificate/index'], resolve),
                      name: 'b_product3',
                      meta: { title: '交易管理', icon: 'dashboard', noCache: true, affix: true, bigScreen: true }
                    },
                    {
                      path: 'listing',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/greenCertificate/listing'], resolve),
                      name: 'b_listing',
                      meta: { title: '售方挂牌情况', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                    },
                    {
                      path: 'wantBuy',
                      hidden: true,
                      component: (resolve) => require(['@/views/bigScreen/greenCertificate/wantBuy'], resolve),
                      name: 'b_wantBuy',
                      meta: { title: '求购动态', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
                    },
                  ]
                }
              ]
              store.commit('SET_BIG_SCREEN', bigScreen);
            }
            accessRoutes.push(store.state.permission.bigScreen[0]);
            // 根据roles权限生成可访问的路由表
            router.addRoutes(accessRoutes); // 动态添加可访问路由表
            store.dispatch('SelectCategorizeDict');
            store.dispatch('SelectProductCategorizeDict');
            next({...to, replace: true}) // hack方法 确保addRoutes已完成
          })
        }).catch(err => {
          store.dispatch('FedLogOut').then(() => {
            Message.error(err);
            next({path: '/'})
          })
        })
      } else {
        next()
        // // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
        // if (hasPermission(store.getters.roles, to.meta.roles)) {
        //   next()
        // } else {
        //   next({ path: '/401', replace: true, query: { noGoBack: true }})
        // }
        //可删 ↑
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
      NProgress.done()
    }
  }
});


router.afterEach(() => {
  NProgress.done()
});
